#四大数据结构CRUD操作

#List(列表）

    #create
    l = [1, 2]
    l2 = l +[3, 4]
    print(l2)
    [1, 2, 3, 4]

    l = [1, 2]
    l = l + [3, 4]
    print(l)
    [1, 2, 3, 4]

    l = [1, 2]
    l += [3, 4]
    print(l)
    [1, 2, 3, 4]

    x = ["a"]
    x = x * 3
    print(x)
    ['a', 'a', 'a']

    x = ["a"]
    x *= 3
    print(x)
    ['a', 'a', 'a']

    x = "a"
    y = ["a"] * 3
    id(x)
    5772960
    id(y[0])
    5772960
    x = "b"
    id(y[0])
    5772960

    x = "你"
    y = ["你"] * 3
    id(x)
    31871776
    id(y[0])
    31872016

    x = ["a"]
    y = [x] * 3
    y
    [['a'], ['a'], ['a']]
    x[0] = "b"
    y
    [['b'], ['b'], ['b']]

    #append(末尾插入值）
    y.append("c")
    y
    [['b'], ['b'], ['b'], 'c']

    #insert(指定位置插入值）
    y.insert(0,"d")
    y
    ['d', ['b'], ['b'], ['b'], 'c']

    #索引（retrieve）
    y[1]
    ['b']
    y[1:3]
    [['b'], ['b']]
    y[-1]
    'c'
    l = list(range(10))
    l
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    l[1:10:2]
    [1, 3, 5, 7, 9]

    l = ["a","b","c"]
    l.index("b")
    1
    l.insert(l.index("b"),"e")
    l
    ['a', 'e', 'b', 'c']

    #update
    l
    ['a', 'e', 'b', 'c']
    l[2:4] = "fg"
    l
    ['a', 'e', 'f', 'g']

    l
    ['a', 'e', 'f', 'g']
    l[2:4] = [1]
    l
    ['a', 'e', 1]

    #delete
    #删除末尾并返回
    l
    ['a', 'e', 1]

    l.pop()
    1
    l
    ['a', 'e']

    #删除索引到的第一个值，不返回
    l
    ['a', 'e']
    l.remove("a")
    l
    ['e']

    #查找重复元素的个数
    l = ["a","a","b","c","a"]
    l.count("a")
    3

    #当前列表做倒序修改，返回None
    l
    ['a', 'a', 'b', 'c', 'a']
    l.reverse()
    l
    ['a', 'c', 'b', 'a', 'a']

    #返回一个新列表
    l
    ['a', 'c', 'b', 'a', 'a']
    l2 = list(reversed(l))
    l2
    ['a', 'a', 'b', 'c', 'a']
    id(l)
    31058960
    id(l2)
    31059760

    #增序排列
    #当前列表做修改，返回None
    l
    [1, 5, 3, 2, 9]
    l.sort()
    l
    [1, 2, 3, 5, 9]

    #返回新列表
    l
    [2, 1, 5, 3, 9]
    l2 = list(sorted(l))
    l2
    [1, 2, 3, 5, 9]
    id(l)
    31058880
    id(l2)
    31059920

#tuple（元组只有索引）

    #retrive
    l = (1,3,4,5)
    l[0]
    1
    l[1:3]
    (3, 4)
    l.index(3)
    1

#dict（字典）
    #create
    #键对值赋值
    d = {"a":1}
    d["b"] = 2
    d
    {'a': 1, 'b': 2}

    #update
    d
    {'a': 1, 'b': 2}
    d_2 = {"c":3,"d":4}
    d.update(d_2)
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4}

    d = {"a":1,"b":2}
    d_2 = {"c":3,"d":4}
    id(d)
    35410832
    id(d_2)
    2150592
    d_3 = d.update(d_2)
    d_3
    id(d_3)
    505743552
    id(None)
    505743552

    #setdefault
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    d.setdefault("z",8)
    8
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8}
    d.setdefault("a",123)
    1
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8}

    #retrieve
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8}
    d["a"]
    1
    d["e"]
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'e'

    d.get("w",22)
    22
    d.get("a",22)
    1

    d.keys()
    dict_keys(['a', 'b', 'c', 'd', 'z'])

    d.values()
    dict_values([1, 2, 3, 4, 8])

    d.items()
    dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('z', 8)])

    #update
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8}
    d["a"] = 14

    d
    {'a': 14, 'b': 2, 'c': 3, 'd': 4, 'z': 8}
    d.update({"a":1,"w":10})
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8, 'w': 10}

    #delete
    d
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'z': 8, 'w': 10}
    d.pop("a")
    1

    d
    {'b': 2, 'c': 3, 'd': 4, 'z': 8, 'w': 10}
    d.popitem()
    ('w', 10)
    d
    {'b': 2, 'c': 3, 'd': 4, 'z': 8}

    d
    {'b': 2, 'c': 3, 'd': 4, 'z': 8}
    d.clear()
    d
    {}

#set(集合）

    #create
    d = {1,2}
    d.add(3)
    d
    {1, 2, 3}

    #retrieve
    1 in d
    True
    5 in d
    False

    #update
    a = {"a","b","c"}
    b = {"x","y","z"}
    a.union(b)
    {'y', 'x', 'b', 'a', 'z', 'c'}
    a
    {'b', 'a', 'c'}
    b
    {'y', 'x', 'z'}
    a.update(b)
    b
    {'y', 'x', 'z'}
    a
    {'y', 'x', 'b', 'a', 'z', 'c'}

    #delete
    a
    {'y', 'x', 'b', 'a', 'z', 'c'}
    a.discard("a")
    a
    {'y', 'x', 'b', 'z', 'c'}
    a.remove("c")
    a
    {'y', 'x', 'b', 'z'}
    a.remove("q")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'q'
    a.discard("q")

    a
    {'y', 'x', 'b', 'z'}
    a.pop()
    'y'